<?php
include 'connect.php';
include 'functions.php';
include 'api.php';

//get list of users
$sqlSelectUsers = mysqli_query($mysqli,"SELECT user.email, user.user_id, user_char.char_id, char_summary.name, char_summary.realm from user INNER JOIN user_char ON user.user_id = user_char.user_id INNER JOIN char_summary ON char_summary.char_id = user_char.char_id");

//get list of users attached to character
//get list of emails
while ($mailRow = mysqli_fetch_array($sqlSelectUsers))
{

	$user_id = $mailRow[1];
	$email = $mailRow[0];
	$char_id = $mailRow[2];
	$char = $mailRow[3];
	$realm = $mailRow[4]; 

	$character = $api->getCharacterApi()->getCharacter($realm, $char, 
		array('guild', 'stats', 'items', 'titles', 'professions', 'talents', 'reputation', 'achievements'));
	include 'updatevars.php';

		
	//check user days
	//get preference for email
	$sqlSelectPrefs = mysqli_query($mysqli,"SELECT value FROM user_preferences WHERE user_id = '$user_id' and preference like 		'%Email_warning%'");
	$sqlPrefsRow  = mysqli_fetch_row($sqlSelectPrefs);
	$emailPref = $sqlPrefsRow[0];
	
	//get warning days
	$sqlSelectPrefs2 = mysqli_query($mysqli,"SELECT value FROM user_preferences WHERE user_id = '$user_id' and preference 			like '%warning_date%'");
	$sqlPrefsRow2  = mysqli_fetch_row($sqlSelectPrefs2);
	$warningPref = $sqlPrefsRow2[0];

	$addString = "P".$warningPref."D";
	$dateCheck = new DateTime($lastLogin);
	$dateCheck->add(new DateInterval($addString));
	$today = new DateTime('today');
	
	$from = 'Mighty WoW Drill<admin@quotationaday.com>';
	$headers = "MIME-Version: 1.0" . "\n";
	$headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";
	$headers .= "From: $from" . "\n";
	$subject = 'Character login reminder';
	$message = '

			<table align="center">
				<tr>
				<td width="600" border="1px solid #E5ECBC" cellspacing="0" cellpadding="0" align="center" bgcolor="#212320" color="#E5ECBC">
				<font face="helvetica" color="#E5ECBC" size="4">
				<table>
				<tr><td bgcolor="#212320" color="#E5ECBC" align="center">
				Here is your character login reminder.<br/>
Your watched character '.$char.' on '.$realm.' has not logged in since '.$lastLogin.'

				</td></tr>
				</table></font>
				</td></tr>
			</table>

';


	if ($dateCheck < $today && $emailPref==1)
	{
		echo "<tr><td>need to send warning email to $email for character $char on $realm (user $user_id)</td></tr><br/>";
		$mail = mail($email,$subject,$message,$headers);
	
	if ($mail=='true')
	{
	echo "<p>mail sent to " . $email . " </p>";
	}
	
	
	}
	else
	{
		echo "<tr><td>no need to send warning email for $char</td></tr><br/>";
	}
}


?>